<?php
function exporttocsv($data,$filename,$head){

    $header = implode("\",\"",array_values($head));
    $header = "\"" .$header;
    $header .= "\"\r\n";
    $content = '';
    $content .= $header;

    header("Expires: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("X-DNS-Prefetch-Control: off");
    header("Cache-Control: private, no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");

    header("Content-Type: application/octet-stream");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".iconv("UTF-8","GB2312//IGNORE",$filename).".csv");

    $content = iconv("UTF-8","GB2312//IGNORE",$content);
    echo $content;
    if($data){
        foreach($data as $val){
            $content = "";
            $new_arr = array();
            foreach ($val as $key => $value)
            {
                if(empty($value)){
                    $value = " ";
                }
                array_push($new_arr, preg_replace("/\"/","\"\"",$value));
            }
            $line = implode("\",\"",$new_arr);
            $line = "\"" .$line;
            $line .= "\"\r\n";

            $content .= $line;
            $content = iconv("UTF-8","GB2312//IGNORE",$content);
            echo  $content;
        }
    }
    exit();
}
$bg = $_GET['bg']." 00:00:00";
$end = $_GET['end']."  23:59:59";
if(empty($bg) || empty($end)){
    echo "请选择时间后再导出";exit();
}
require_once ('class.db.php');
$db = new DB();
$where = " time >= '".$bg."' AND time <= '".$end."'";
$res = $db->get_results('select * from dk_msg WHERE '.$where);
$head = array("序号","电话","需求","姓名","时间");
$filename = "用户贷款信息".date('Ymd');
exporttocsv($res,$filename,$head);

?>